package com.qczl.pme.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.qczl.pme.dao.CameraDao;
import com.qczl.pme.dao.PersonDao;
import com.qczl.pme.dao.SignDao;
import com.qczl.pme.dao.StbDao;
import com.qczl.pme.dao.VideoDao;
import com.qczl.pme.entity.PMECamera;
import com.qczl.pme.entity.PMEPerson;
import com.qczl.pme.entity.PMESTB;
import com.qczl.pme.entity.PMESewise;
import com.qczl.pme.entity.PMESign;
import com.qczl.pme.entity.PMEVideo;
import com.qczl.pme.service.SignService;
import com.qczl.pme.vo.SignVo;
import com.qczl.util.ConfigUtil;
import com.qczl.util.DateUtil;
import com.qczl.util.HttpClientUtil;

/**
 * 签到业务逻辑层实现类
 * 
 * @author SongR
 *
 */
@Service
public class SignServiceImpl implements SignService {

	Logger log = Logger.getLogger(this.getClass());

	@Autowired
	StbDao stbdao;
	@Autowired
	PersonDao persondao;
	@Autowired
	CameraDao cameradao;
	@Autowired
	SignDao signdao;
	@Autowired
	VideoDao videodao;

	/**
	 * 实现驻村干部视频签到
	 * 
	 * @version v1
	 */
	@Override
	public SignVo videoZhucunV1(String stbno, String phone) throws Exception {
		PMESTB stb = stbdao.get("from PMESTB where no = '" + stbno + "' and status = 0");
		if (null != stb) {
			PMEPerson person = persondao.get("from PMEPerson where phone=" + phone + "and status=0 ");
			PMECamera camera = cameradao.get("from PMECamera where id=" + stb.getCameraid() + " and status=0");
			PMESewise sewise = camera.getSewise();
			String port = "";
			if (sewise.getProt2() != null && !sewise.getProt2().equals("")) {
				port += ":" + sewise.getProt2();
			}
			final String url = "http://" + sewise.getIp() + port + "/record";
			Map<String, String> map = new HashMap<String, String>();
			map.put("do", "start");
			map.put("streamid", camera.getStreamId());
			map.put("callback", ConfigUtil.getByKey("config", "sewise.zhucuncallback"));
			String result = HttpClientUtil.doPost(url, map);
			final JSONObject j = JSONObject.parseObject(result);
			if (null != j) {
				if (j.getString("success") == "true") {
					PMESign sign = new PMESign();
					sign.setPersonid(person.getId());
					sign.setSigntime(new Date());
					sign.setSigntypeid(new Long(2));
					sign.setStbno(stbno);
					sign.setState(0);
					sign.setTaskid(j.getString("taskid"));
					signdao.save(sign);
					new Timer().schedule(new TimerTask() {
						@Override
						public void run() {
							Map<String, String> map = new HashMap<String, String>();
							map.put("do", "stop");
							map.put("taskid", j.getString("taskid"));
							HttpClientUtil.doPost(url, map);
						}
					}, new Long(ConfigUtil.getByKey("config", "sewise.zhucunduration")));
					SignVo vo = new SignVo();
					vo.setPersonid(person.getId());
					vo.setName(person.getName());
					vo.setIdCard(person.getIdCard());
					return vo;
				} else {
					return null;
				}
			} else {
				return null;
			}
		}
		return null;
	}

	/**
	 * 实现获取签到人员信息
	 * 
	 * @version v1
	 */
	@Override
	public List<SignVo> getZhucunSignV1(String stbno, String start, String size) throws Exception {
		String hql = "from PMESign where signtypeid=2 and state=0 and stbno='" + stbno + "' and signtime > '"
				+ DateUtil.startTime() + "' and signtime < '" + DateUtil.endTime() + "'";
		hql += " group by personid  order by signtime ";
		List<PMESign> signs = new ArrayList<PMESign>();
		if (start != null && !"".equals(start) && null != size) {
			signs = signdao.findP(hql, Integer.parseInt(start), Integer.parseInt(size));
		} else {
			signs = signdao.find(hql);
		}
		List<SignVo> vos = new ArrayList<SignVo>();
		for (PMESign pmeSign : signs) {
			PMEPerson person = persondao.get(PMEPerson.class, pmeSign.getPersonid());
			SignVo vo = new SignVo();
			vo.setPersonid(person.getId());
			vo.setIdCard(person.getIdCard());
			vo.setName(person.getName());
			vo.setSigntime(pmeSign.getSigntime());
			vos.add(vo);
		}
		return vos;
	}

	/**
	 * 实现获取驻村干部视频签到人员数量
	 * 
	 * @version v1
	 */
	@Override
	public Long countZhucunSignV1(String stbno) throws Exception {
		String hql = "select count(*) from PMESign where signtypeid=2 and state=0 and stbno='" + stbno
				+ "' and signtime > '" + DateUtil.startTime() + "' and signtime < '" + DateUtil.endTime()
				+ "'  group by personid";
		return signdao.countG(hql);
	}

	/**
	 * 活动签到
	 */
	@Override
	public SignVo videoHuodongV1(String stbno, String phone) throws Exception {
		PMESTB stb = stbdao.get("from PMESTB where no = '" + stbno + "' and status = 0");
		if (null != stb) {
			PMEPerson person = persondao.get("from PMEPerson where phone=" + phone + "and status=0 ");
			PMECamera camera = cameradao.get("from PMECamera where id=" + stb.getCameraid() + " and status=0");
			PMESewise sewise = camera.getSewise();
			String port = "";
			if (sewise.getProt2() != null && !sewise.getProt2().equals("")) {
				port += ":" + sewise.getProt2();
			}
			final String url = "http://" + sewise.getIp() + port + "/record";
			Map<String, String> map = new HashMap<String, String>();
			map.put("do", "start");
			map.put("streamid", camera.getStreamId());
			map.put("callback", ConfigUtil.getByKey("config", "sewise.huodongcallback"));
			String result = HttpClientUtil.doPost(url, map);
			final JSONObject j = JSONObject.parseObject(result);
			if (null != j) {
				if (j.getString("success") == "true") {
					PMESign sign = new PMESign();
					sign.setPersonid(person.getId());
					sign.setSigntime(new Date());
					sign.setSigntypeid(new Long(3));
					sign.setStbno(stbno);
					sign.setState(0);
					sign.setTaskid(j.getString("taskid"));
					signdao.save(sign);
					new Timer().schedule(new TimerTask() {
						@Override
						public void run() {
							Map<String, String> map = new HashMap<String, String>();
							map.put("do", "stop");
							map.put("taskid", j.getString("taskid"));
							HttpClientUtil.doPost(url, map);
						}
					}, new Long(ConfigUtil.getByKey("config", "sewise.zhucunduration")));
					SignVo vo = new SignVo();
					vo.setPersonid(person.getId());
					vo.setName(person.getName());
					vo.setIdCard(person.getIdCard());
					return vo;
				} else {
					return null;
				}
			} else {
				return null;
			}
		}
		return null;
	}

	@Override
	public List<SignVo> getHuodongSignV1(String stbno, String start, String size) {
		String hql = "from PMESign where signtypeid=3 and state=0 and stbno='" + stbno + "' and signtime > '"
				+ DateUtil.startTime() + "' and signtime < '" + DateUtil.endTime() + "'";
		hql += " group by personid  order by signtime ";
		List<PMESign> signs = new ArrayList<PMESign>();
		if (start != null && !"".equals(start) && null != size) {
			signs = signdao.findP(hql, Integer.parseInt(start), Integer.parseInt(size));
		} else {
			signs = signdao.find(hql);
		}
		List<SignVo> vos = new ArrayList<SignVo>();
		for (PMESign pmeSign : signs) {
			PMEPerson person = persondao.get(PMEPerson.class, pmeSign.getPersonid());
			SignVo vo = new SignVo();
			vo.setPersonid(person.getId());
			vo.setIdCard(person.getIdCard());
			vo.setName(person.getName());
			vo.setSigntime(pmeSign.getSigntime());
			vos.add(vo);
		}
		return vos;
	}

	@Override
	public Long countHuodongSignV1(String stbno) {
		String hql = "select count(*) from PMESign where signtypeid=3 and state=0 and stbno='" + stbno
				+ "' and signtime > '" + DateUtil.startTime() + "' and signtime < '" + DateUtil.endTime()
				+ "'  group by personid";
		return signdao.countG(hql);
	}

	/**
	 * 实现获取坐班签到记录
	 */
	@Override
	public List<SignVo> signZuobanV1(String stbno, String personid, int start, int size) throws Exception {
		String hql = "select a from PMESign a,PMESTB b where a.signtypeid=2 and a.stbno=b.no and a.state=0 and b.status=0 and b.no='"
				+ stbno + "' and a.personid=" + personid + " order by a.signtime desc";
		List<PMESign> signArray = signdao.findP(hql, start, size);
		List<SignVo> vos = new ArrayList<SignVo>();
		for (PMESign pmeSign : signArray) {
			PMEPerson person = persondao.get(PMEPerson.class, pmeSign.getPersonid());
			PMEVideo video = videodao.get("from PMEVideo where taskId='" + pmeSign.getTaskid() + "'");
			PMESTB stb = stbdao.get("from PMESTB where no = '" + pmeSign.getStbno() + "' and status = 0");
			PMECamera camera = cameradao.get("from PMECamera where id=" + stb.getCameraid());
			String url = "http://" + camera.getSewise().getIp();
			if (camera.getSewise().getProt2() != null && !"".equals(camera.getSewise().getProt2())) {
				url += ":" + camera.getSewise().getProt2();
			}
			SignVo vo = new SignVo();
			vo.setUrl(url + video.getPlayUrl());
			vo.setPersonid(person.getId());
			vo.setIdCard(person.getIdCard());
			vo.setName(person.getName());
			vo.setSigntime(pmeSign.getSigntime());
			vos.add(vo);
		}
		return vos;
	}

	/**
	 * 实现获取坐班签到数
	 */
	@Override
	public Long countSignZuobanv1(String stbno, String personid) throws Exception {
		String hql = "select count(*) from PMESign a,PMESTB b where a.signtypeid=2 and a.stbno=b.no and a.state=0 and b.status=0 and b.no='"
				+ stbno + "' and a.personid=" + personid;
		return signdao.count(hql);
	}

	/**
	 * 实现获取活动签到记录
	 */
	@Override
	public List<SignVo> signHuodongV1(String stbno, String personid, int start, int size) throws Exception {
		String hql = "select a from PMESign a,PMESTB b where a.signtypeid=3 and a.stbno=b.no and a.state=0 and b.status=0 and b.no='"
				+ stbno + "' and a.personid=" + personid + " order by a.signtime desc";
		List<PMESign> signArray = signdao.findP(hql, start, size);
		List<SignVo> vos = new ArrayList<SignVo>();
		for (PMESign pmeSign : signArray) {
			PMEPerson person = persondao.get(PMEPerson.class, pmeSign.getPersonid());
			SignVo vo = new SignVo();
			vo.setPersonid(person.getId());
			vo.setIdCard(person.getIdCard());
			vo.setName(person.getName());
			vo.setSigntime(pmeSign.getSigntime());
			vos.add(vo);
		}
		return vos;
	}

	/**
	 * 实现获取活动签到数
	 */
	@Override
	public Long countSignHuodongv1(String stbno, String personid) throws Exception {
		String hql = "select count(*) from PMESign a,PMESTB b where a.signtypeid=3 and a.stbno=b.no and a.state=0 and b.status=0 and b.no='"
				+ stbno + "' and a.personid=" + personid;
		return signdao.count(hql);
	}

	/**
	 * 实现获取会议签到记录
	 */
	@Override
	public List<SignVo> signHuiyiV1(String stbno, String personid, int start, int size) throws Exception {
		String hql = "select a from PMESign a,PMESTB b where a.signtypeid=1 and a.stbno=b.no and a.state=0 and b.status=0 and b.no='"
				+ stbno + "' and a.personid=" + personid + " order by a.signtime desc";
		List<PMESign> signArray = signdao.findP(hql, start, size);
		List<SignVo> vos = new ArrayList<SignVo>();
		for (PMESign pmeSign : signArray) {
			PMEPerson person = persondao.get(PMEPerson.class, pmeSign.getPersonid());
			SignVo vo = new SignVo();
			vo.setPersonid(person.getId());
			vo.setIdCard(person.getIdCard());
			vo.setName(person.getName());
			vo.setSigntime(pmeSign.getSigntime());
			vos.add(vo);
		}
		return vos;
	}

	@Override
	public Long countSignHuiyiv1(String stbno, String personid) throws Exception {
		String hql = "select count(*) from PMESign a,PMESTB b where a.signtypeid=1 and a.stbno=b.no and a.state=0 and b.status=0 and b.no='"
				+ stbno + "' and a.personid=" + personid;
		return signdao.count(hql);
	}

}
